package open.tech.dawn.rest.common.util;

import cn.hutool.core.io.IoUtil;
import com.alibaba.fastjson.JSONObject;
import lombok.extern.slf4j.Slf4j;
import open.tech.dawn.core.error.ServiceException;

import javax.servlet.http.HttpServletRequest;
import java.io.InputStream;
import java.nio.charset.StandardCharsets;

@Slf4j
public class RequestUtil {

    public static JSONObject getJSONBody(HttpServletRequest request) {
        InputStream inputStream = null;
        try {
            inputStream = request.getInputStream();
            return JSONObject.parseObject(IoUtil.read(inputStream, StandardCharsets.UTF_8));
        } catch (Exception e) {
            log.error("解析请求body数据异常", e);
            throw new ServiceException("请求异常");
        } finally {
            IoUtil.close(inputStream);
        }
    }

}
